package com.wx;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;

public class UserService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("用户输入用户名是："+username);
        /**
         * 传入了用户名，然后根据用户名去数据库查询
         * 返回用户信息（包含用户的密码信息）
         * 框架会进行密码比对
         */
        String usernameMy="root";
        String passwordMy="{noop}"+"root";
        if(username.equals(usernameMy)){
            // 如果依据用户名查询到了信息会返回密码与权限信息
            // 传经来的这个用户名所拥有的权限列表
            ArrayList<GrantedAuthority> list = new ArrayList<>();
            // 添加当前用户的角色或权限
            // 如果是ROLE开始表示角色，如果不是表示权限
            list.add(new SimpleGrantedAuthority("permission_A"));// 授权
            list.add(new SimpleGrantedAuthority("permission_B"));// 授权
            list.add(new SimpleGrantedAuthority("permission_D"));// 授权
            list.add(new SimpleGrantedAuthority("ROLE_C")); // 授予一个角色
            User user = new User(username, passwordMy, list);

            return user;
        }
        return null;
    }
}
